package com.luania.todolist2.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.luania.todolist2.Adapter.IconAdapter;
import com.luania.todolist2.R;
import com.luania.todolist2.Model.ImageModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    List<ImageModel> images = new ArrayList<>();
    Map<String, Integer> map = new HashMap<>();
    ListView lv_main_icon;
    private int iconId;
    private String iconName;
    IconAdapter iconAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //用toolbar代替原来的actionbar
        setSupportActionBar(toolbar);

        lv_main_icon = (ListView) findViewById(R.id.lv_main_icon);


        iconAdapter = new IconAdapter(images, MainActivity.this);
        initImages();

        lv_main_icon.setAdapter(iconAdapter);

        lv_main_icon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, CotainsActivity.class);
                intent.putExtra("groupName", images.get(position).getImageName());
                startActivity(intent);

            }
        });
// 点击事件，打开任务项目.
    }

    @Override
    protected void onResume() {
        super.onResume();
        images.clear();
        initImages();

        iconAdapter.notifyDataSetChanged();
    }

    private void initImages() {
        SharedPreferences sharedPreferences = getSharedPreferences("group", MODE_PRIVATE);
        map = (Map) sharedPreferences.getAll();
        Set<String> keySet = map.keySet();
        for (String key : map.keySet()) {
            iconId = map.get(key);
            iconName = key;
            ImageModel imageModel = new ImageModel(iconId, iconName);
            images.add(imageModel);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //将R.menu.main的资源注入menu
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_add_group) {
            //设置从哪个activity跳转到哪个activity
            Intent intent = new Intent(this, AddGroupActivity.class);
            //开启一个新的activity
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
